package com.appspot.showup_app.showup;


import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.TextView;

import com.appspot.showup_app.showup.model.ShowUpCustomerMessage;

import java.util.ArrayList;

public class CustomersArrayAdapter extends BaseAdapter implements Filterable {

    final String TAG = "CustomersArrayAdapter";

    Context mContext;

    private ArrayList<ShowUpCustomerMessage> items;
    private ArrayList<ShowUpCustomerMessage> itemsAll;
    private ArrayList<ShowUpCustomerMessage> suggestions;
    private LayoutInflater mInflater;

    public CustomersArrayAdapter(Context mContext, ArrayList<ShowUpCustomerMessage> items) {

        mInflater = LayoutInflater.from(mContext);

        this.items = items;
        this.itemsAll = (ArrayList<ShowUpCustomerMessage>) items.clone();
        this.suggestions = new ArrayList<ShowUpCustomerMessage>();
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        try{
            if(convertView ==null){
                // inflate the layout
                convertView = mInflater.inflate(R.layout.customer_list_item, parent, false);
                ImageButton btnEdit = (ImageButton) convertView.findViewById(R.id.btnEdit);
                ImageButton btnDelete = (ImageButton) convertView.findViewById(R.id.btnDelete);
                btnDelete.setVisibility(View.GONE);
                btnEdit.setVisibility(View.GONE);
            }

            // object item based on the position
            ShowUpCustomerMessage customer = items.get(position);

            // get the TextView and then set the text (item name) and tag (item ID) values
            TextView phone = (TextView) convertView.findViewById(R.id.phone);
            TextView name = (TextView) convertView.findViewById(R.id.name);

            phone.setText(customer.getPhoneNumber());
            name.setText(customer.getName());
            convertView.setTag(customer);
            // in case you want to add some style, you can do something like:

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;

    }


    @Override
    public Filter getFilter() {
        return nameFilter;
    }

    Filter nameFilter = new Filter() {
        @Override
        public String convertResultToString(Object resultValue) {
            String str = ((ShowUpCustomerMessage)(resultValue)).getName();
            return str;
        }
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if(constraint != null) {
                suggestions.clear();
                for (ShowUpCustomerMessage customer : itemsAll) {
                    if(customer.getName().toLowerCase().startsWith(constraint.toString().toLowerCase())){
                        suggestions.add(customer);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            ArrayList<ShowUpCustomerMessage> filteredList = (ArrayList<ShowUpCustomerMessage>) results.values;
            if(results != null && results.count > 0) {
                items.clear();
                for (ShowUpCustomerMessage c : filteredList) {
                    items.add(c);
                }
                notifyDataSetChanged();
            }
        }
    };
}